<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notify extends CI_Controller{
	/**
     * 构造函数
     *
     * @access  public
     * @return  void
     */
	public function __construct(){
		parent::__construct();
		$this->user_name = $this->session->userdata('user_name');
		$this->user_id = $this->session->userdata('user_id');
		$this->CURD = date("Y-m-d");
	}

	//截取固定长度的标题
	function mysub($str,$start=0,$length=60,$left="...",$charset="utf-8"){
		if($charset=="utf-8"){
			$index=0; //当前字符的第一字节位置
			$index1=0; //当前字符的最后字节位置
			$result="";
			$haslen=0; //已经截取的字节
			for($i=0;$i<$length;$i++){
				$index1=$index;
				$len=0;
				$len1=0;
				$first_b=substr($str,$index,1);
				if(ord($first_b)>224){
					if($i>=$start){
						$len=3;
						$len1=3;
					}
					$index+=$len;
					$haslen+=$len;
				 }elseif(ord($first_b)>192){
					if($i>=$start) {
						$len=2;
						$len1=2;
					}
					$index+=$len;
					$haslen+=$len;
				}else{
					if($i>=$start) {
						$len=1;
						$len1=1.5; //这里引文显示的时候2个是一个中文的长度所以我们定为1.5因为中文是3
					}
					$index+=$len;
					$haslen+=$len1;
				 }
				 if($haslen<=$length){
					$result.=substr($str,$index1,$len);
				 }else{
					break;
				 }
			}
		}else{
			$result=mb_substr($str,$start,$length,$charset);
		}
		if($result!=$str){
		   $result.=$left;
		}
		return $result;
	}

	//获取通知公告列表方法
	function get_jsonp_list(){
		$content = array();
		//$username = $this->input->get('username');
		//$username = urldecode($username);
		
		$userid = $this->input->get('userid');		
		$StartNum = $this->input->get('page');
		//$username = $this->input->get('username');
		//$StartNum = $this->input->get('StartNum'); 
		$pagesize=10;
		if($StartNum < 0){
			$StartNum = 1;
		}
		$start=($StartNum-1)*$pagesize;
		
		$this->db->where('begin_date <=', $this->CURD);
		//$this->db->where('begin_date <>', '0000-00-00');
		$this->db->where('end_date >=', $this->CURD);
		//$this->db->or_where('end_date', '0000-00-00');
		$this->db->like('to_id', $userid.',');
		
		$this->db->order_by("notify_id", 'desc'); 
		$this->db->limit($pagesize,$start);
		$query=$this->db->get('notify');
		//echo $this->db->last_query();//输出随后一次查询的语
		foreach ($query->result() as $v){
			$st = reset(explode(' ',$v->sendertime));
			$isa = $v->attachment_id?1:0;
			$content[] = array("id"=>$v->notify_id,"fn"=>$v->from_name,"t"=>$this->mysub($v->title,0,51),"st"=>$st,"dn"=>$v->dept_name,'isa'=>$isa);

		}
		
		$arrset['result'] = $content;
		//$arrset['a_count'] = count($content);
		//$arrset['startNum'] = ++$StartNum;
		
		$result = json_encode($arrset);
		//$callback=$_GET['jsoncallback'];  
		$callback=$this->input->get('callback');
		echo $callback."($result)";  
	}

	function get_jsonp_content(){
		//取详细内容
		$notify_id = $this->input->get('notify_id');
		$this->db->where('notify_id', $notify_id);
		$this->db->limit(1);
		$query=$this->db->get('notify');
		$row = $query->first_row();
		$content = strip_tags($row->content,"<br>");
		$arrset = array(
			"notify_id"=>$row->notify_id,
			"from_name"=>$row->from_name,
			"dept_name"=>$row->dept_name,
			"title"=>$row->title,
			"content"=>$content,
			"sendertime"=>$row->sendertime
		);

		if($row->attachment_id != ''){
			$moduletype = "notify";
			$attach_id = substr($row->attachment_id,0,-1);
			$attach_name = substr($row->attachment_name,0,-1);
			$arrset['attach_id'] = explode(",",$attach_id);
			$arrset['attach_name'] = explode("*",$attach_name);
		}  
		
		$result = json_encode($arrset);
		//$callback=$_GET['jsoncallback'];  
		$callback=$this->input->get('jsoncallback');
		echo $callback."($result)"; 

	}//end 收件箱方法结束
	

}